package com.example.gaojt.androiddemo.boostDemo.aa;

import android.app.WallpaperInfo;
import android.app.WallpaperManager;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.text.TextUtils;

import java.lang.reflect.Field;


public class PackageUtils {


    /**
     * 获取当前动态壁纸的包名
     *
     * @param context the context
     * @return 动态壁纸的包名，或者""
     */
    public final static String getPackageNameOfCurrentLiveWallpaper(Context context) {
        try {
            final WallpaperManager wallpaperManager = WallpaperManager.getInstance(context);
            if (wallpaperManager == null) {
                return "";
            }

            WallpaperInfo wi = wallpaperManager.getWallpaperInfo();
            if (wi == null) {
                return "";
            }

            String pkgname = wallpaperManager.getWallpaperInfo().getPackageName();
            return pkgname == null ? "" : pkgname;
        } catch (Exception e) {}
        return "";
    }

    public static ApplicationInfo getApplicationInfo(Context context, String pkgName) {

        if (TextUtils.isEmpty(pkgName)) {
            return null;
        }

        try {
            PackageInfo pkgInfo = context.getPackageManager().getPackageInfo(pkgName, 0);
            if (pkgInfo != null) {
                ApplicationInfo appInfo = pkgInfo.applicationInfo;
                return appInfo;
            }
        } catch (Exception e) {}

        return null;
    }


    public static boolean isCoreApp(PackageInfo info) {
        boolean coreApp = false;
        try {
            Field field = PackageInfo.class.getField("coreApp");
            field.setAccessible(true);
            coreApp = field.getBoolean(info);
        } catch (Exception e) {

        }
        return coreApp;
    }

    public static boolean isPersistentApp(PackageInfo info) {
        if ((info.applicationInfo.flags
                & (ApplicationInfo.FLAG_SYSTEM | ApplicationInfo.FLAG_PERSISTENT)) == (ApplicationInfo.FLAG_SYSTEM
                        | ApplicationInfo.FLAG_PERSISTENT)) {
            return true;
        } else {
            return false;
        }
    }
}
